﻿using System.Text.RegularExpressions;
using Lucid.ESA.ClientDataSystem.Domain.Common;
using Lucid.ESA.ClientDataSystem.Domain.ParameterClasses;

namespace Lucid.ESA.ClientDataSystem.Domain.Entities
{
    public class Contact : Entity
    {
        protected string _title;
        protected string _forename;
        protected string _surname;
        protected string _emailAddress;
        protected string _telephoneNumber;
        protected bool _deleted;
        protected Client _client;

        public virtual string Title
        {
            get { return _title; }
            set { _title = value; }
        }

        public virtual string Forename
        {
            get { return _forename; }
            set { _forename = value; }
        }

        public virtual string Surname
        {
            get { return _surname; }
            set { _surname = value; }
        }

        public virtual string EmailAddress
        {
            get { return _emailAddress; }
            set { _emailAddress = value; }
        }

        public virtual string TelephoneNumber
        {
            get { return _telephoneNumber; }
            set { _telephoneNumber = value; }
        }

        public virtual bool Deleted
        {
            get { return _deleted; }
            set { _deleted = value; }
        }

        public virtual Client Client
        {
            get { return _client; }
            set { _client = value; }
        }

        public virtual string FullName
        {
            get
            {
                //This is a derived field: calculate full name from title, forename and surname.
                return ((_title == null ? "" : _title + " ") +
                    (_forename == null ? "" : _forename + " ") +
                    _surname).Trim();
            }
        }

        public static Contact Create(CreateContactParameters parameters)
        {
            Contact contact = new Contact();
            contact.Title = parameters.Title;
            contact.Forename = parameters.Forename;
            contact.Surname = parameters.Surname;
            contact.EmailAddress = parameters.EmailAddress;
            contact.TelephoneNumber = parameters.TelephoneNumber;
            contact.Deleted = false;
            contact.Client = parameters.Client;

            if(contact.Client != null)
                contact.Client.Contacts.Add(contact);

            return contact;
        }

        public virtual void Edit(EditContactParameters parameters)
        {
            _title = parameters.Title;
            _forename = parameters.Forename;
            _surname = parameters.Surname;
            _emailAddress = parameters.EmailAddress;
            _telephoneNumber = parameters.TelephoneNumber;
        }

        public virtual void Delete()
        {
            _deleted = true;
        }

        public virtual ValidationMessageCollection Validate()
        {
            ValidationMessageCollection messages = new ValidationMessageCollection();

            //Surname is required.
            if (_surname == null || _surname == string.Empty)
                messages.AddError("Surname", "Surname cannot be empty.");

            //If email address is provided, it must be a valid format.
            if (_emailAddress != null)
            {
                Regex rxEmailAddress = new Regex(@"^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$");
                if (!rxEmailAddress.IsMatch(_emailAddress.ToUpper()))
                {
                    messages.AddError("EmailAddress", "Invalid email address.");
                }
            }

            //If telephone number is provided, it must be a valid format.
            if (_telephoneNumber != null)
            {
                Regex rxEmailAddress = new Regex(@"^\+?[\d ]+$");
                if (!rxEmailAddress.IsMatch(_telephoneNumber))
                {
                    messages.AddError("TelephoneNumber", "Invalid telephone number.");
                }
            }

            //Client is required.
            if (_client == null)
                messages.AddError("Client", "Client is not set.");

            return messages;
        }
    }
}
